from PyQt5.QtWidgets import QApplication, QMainWindow, QSplitter, QTreeView, QLineEdit, QComboBox, QPushButton, \
    QTableWidget, QVBoxLayout, QWidget, QHBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QFont
import sys

# 假设 load_data_from_json 和 create_tree_model 已正确实现
from Law_SOP_Listorg import load_data_from_json, create_tree_model


class LawWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('PyQt5 前端界面')
        self.setGeometry(100, 50, 1600, 900)

        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        layout = QVBoxLayout(central_widget)

        control_layout = QHBoxLayout()
        layout.addLayout(control_layout)

        self.search_layout1 = QHBoxLayout()
        control_layout.addLayout(self.search_layout1)

        self.search_line1 = QLineEdit()
        self.search_line1.setPlaceholderText("搜索公司")
        self.search_line1.setFixedHeight(40)
        font = QFont()
        font.setPointSize(12)
        self.search_line1.setFont(font)
        self.search_layout1.addWidget(self.search_line1)
        self.search_line1.textChanged.connect(self.on_search_changed1)

        # 新增搜索栏
        self.search_layout3 = QHBoxLayout()
        control_layout.addLayout(self.search_layout3)

        self.search_line3 = QLineEdit()
        self.search_line3.setPlaceholderText("搜索业务")
        self.search_line3.setFixedHeight(40)
        self.search_line3.setFont(font)
        self.search_layout3.addWidget(self.search_line3)
        self.search_line3.textChanged.connect(self.on_search_changed2)

        self.search_layout2 = QHBoxLayout()
        control_layout.addLayout(self.search_layout2)

        self.search_line2 = QLineEdit()
        self.search_line2.setPlaceholderText("搜索条目")
        self.search_line2.setFixedHeight(40)
        self.search_line2.setFont(font)
        self.search_layout2.addWidget(self.search_line2)
        self.search_line2.textChanged.connect(self.on_search_changed3)

        control_layout.addStretch(1)

        self.combo_box = QComboBox()
        self.combo_box.addItem("实体制度")
        self.combo_box.addItem("程序制度")
        control_layout.addWidget(self.combo_box)

        self.default_button = QPushButton("还原默认")
        control_layout.addWidget(self.default_button)

        self.generate_button = QPushButton("生成")
        control_layout.addWidget(self.generate_button)

        self.hsplitter = QSplitter(Qt.Horizontal)
        layout.addWidget(self.hsplitter)

        self.tree_view1 = QTreeView()
        self.hsplitter.addWidget(self.tree_view1)

        self.tree_view2 = QTreeView()
        self.hsplitter.addWidget(self.tree_view2)

        self.table_widget = QTableWidget()
        self.hsplitter.addWidget(self.table_widget)

        self.tree_view3 = QTreeView()
        self.hsplitter.addWidget(self.tree_view3)

        data = load_data_from_json('org.json')
        self.model1 = create_tree_model(data)
        self.tree_view1.setModel(self.model1)

        self.hsplitter.setSizes([100, 100, 300, 300])
        # 连接分割器移动信号以调整搜索栏宽度
        self.hsplitter.splitterMoved.connect(self.adjust_search_layouts)
        # 首次调整搜索栏宽度以适应分割器列宽
        self.show()
        self.adjust_search_layouts(None, None)

    def on_search_changed1(self, text):
        print("搜索公司:", text)
        self.model1.search(text)
        root_item = self.model1.invisibleRootItem()
        for i in range(root_item.rowCount()):
            child_item = root_item.child(i)
            self.expand_item(child_item)

    def expand_item(self, item):
        index = self.model1.indexFromItem(item)
        self.tree_view1.expand(index)
        if item.hasChildren():
            for i in range(item.rowCount()):
                self.expand_item(item.child(i))  # 调用SearchableModel的search方法进行搜索

    def on_search_changed2(self, text):
        # 实现搜索功能
        print("搜索业务:", text)

    def on_search_changed3(self, text):
        # 实现搜索功能
        print("搜索条目:", text)

    def adjust_search_layouts(self, index, pos):
        sizes = self.hsplitter.sizes()
        self.search_line1.setFixedWidth(sizes[0])
        self.search_line3.setFixedWidth(sizes[1])
        self.search_line2.setFixedWidth(sizes[2])


if __name__ == '__main__':
    app = QApplication(sys.argv)
    main_window = LawWindow()
    main_window.show()
    sys.exit(app.exec_())